<template>
  <div class="select-heroe">
    <div class="search">
      <div class="icon">
        <img src="../../../../assets/ss.svg" alt="" />
      </div>
      <input type="text" v-model="search" placeholder="请输入武将名称" />
      <div class="clear" v-if="search" @click="search = ''">
        <img src="../../../../assets/clear.svg" alt="" />
      </div>
    </div>
    <collapse-transition>
      <div class="country" v-show="!search.trim()">
        <div
          class="item"
          @click="selectCountry(1)"
          :class="{ active: filter.country.indexOf(1) !== -1 }"
        >
          魏
        </div>
        <div
          class="item"
          @click="selectCountry(2)"
          :class="{ active: filter.country.indexOf(2) !== -1 }"
        >
          蜀
        </div>
        <div
          class="item"
          @click="selectCountry(3)"
          :class="{ active: filter.country.indexOf(3) !== -1 }"
        >
          吴
        </div>
        <div
          class="item"
          @click="selectCountry(4)"
          :class="{ active: filter.country.indexOf(4) !== -1 }"
        >
          群
        </div>
        <div class="flex"></div>
        <div
          class="filter"
          @click="filter.show = !filter.show"
          :class="{ active: filter.show }"
        >
          筛选<img
            src="../../../../assets/down.svg"
            onload="SVGInject(this)"
            class="down-icon"
            :class="{ active: filter.show }"
          />
        </div>
      </div>
    </collapse-transition>
    <collapse-transition>
      <div class="quality" v-show="filter.show && !search.trim()">
        <div class="tit">·武将品质</div>
        <div class="list">
          <div
            class="item"
            @click="selectQuality(4)"
            :class="{ active: filter.quality.indexOf(4) !== -1 }"
          >
            S
          </div>
          <div
            class="item"
            @click="selectQuality(3)"
            :class="{ active: filter.quality.indexOf(3) !== -1 }"
          >
            A
          </div>
          <div
            class="item"
            @click="selectQuality(2)"
            :class="{ active: filter.quality.indexOf(2) !== -1 }"
          >
            B
          </div>
          <div
            class="item"
            @click="selectQuality(1)"
            :class="{ active: filter.quality.indexOf(1) !== -1 }"
          >
            C
          </div>
        </div>
      </div>
    </collapse-transition>
    <div class="heroes-list">
      <vue-scroll :ops="ops">
        <div class="list">
          <div
            class="heroe"
            @click="clickHeroe(heroe)"
            v-for="(heroe, i) in filterList"
            :key="i"
          >
            <div
              class="radio"
              :class="{
                active: heroeIsSelect(heroe.id),
                dis: heroeIsSelect(heroe.id) !== fill,
              }"
            >
              <van-icon name="success" />
            </div>
            <div class="img">
              <img :src="heroe.avatar" :type="heroe.quality" />
            </div>
            <div class="name"><p v-html="heroe.name"></p></div>
          </div>
        </div>
      </vue-scroll>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import Heroe from "@/object/Heroe";
export default {
  name: "SelectHeroe",
  props: {
    value: Object,
    fill: String,
  },
  model: { prop: "value", event: "change" },
  computed: {
    ...mapState("data", ["heroesList", "userHeroes"]),
    filterList() {
      if (this.search.trim()) {
        let list = this.heroesList
          .map((item) => {
            return new Heroe(JSON.parse(JSON.stringify(item)));
          })
          .filter((item) => {
            if (item.name.indexOf(this.search.trim()) !== -1) {
              var reg = new RegExp(this.search.trim(), "g");
              item.name = item.name.replace(
                reg,
                "<span>" + this.search.trim() + "</span>"
              );
              return true;
            } else return false;
          });
        return list;
      } else {
        let list = this.heroesList.map((item) => {
          return new Heroe(JSON.parse(JSON.stringify(item)));
        });

        // 国家筛选
        if (this.filter.country.length) {
          list = list.filter((item) => {
            return this.filter.country.indexOf(item.country) !== -1;
          });
        }

        // 品质筛选
        if (this.filter.quality.length) {
          list = list.filter((item) => {
            return this.filter.quality.indexOf(item.quality) !== -1;
          });
        }

        return list;
      }
    },
  },
  data() {
    return {
      search: "",
      ops: {
        scrollPanel: {
          scrollingX: false,
          scrollingY: true,
        },
        bar: {
          showDelay: 500,
          onlyShowBarOnScroll: true,
          keepShow: false,
          background: "#c1c1c1",
          opacity: 1,
          hoverStyle: false,
          specifyBorderRadius: false,
          minSize: false,
          size: "5px",
          disable: false,
        },
      },
      filter: {
        show: false,
        quality: [],
        country: [],
      },
    };
  },
  methods: {
    selectCountry(country) {
      if (this.filter.country.indexOf(country) === -1) {
        this.filter.country.push(country);
      } else {
        this.filter.country.splice(this.filter.country.indexOf(country), 1);
      }
    },
    selectQuality(quality) {
      if (this.filter.quality.indexOf(quality) === -1) {
        this.filter.quality.push(quality);
      } else {
        this.filter.quality.splice(this.filter.quality.indexOf(quality), 1);
      }
    },
    // 武将是否被选中
    heroeIsSelect(id) {
      if (this.value.chef_hero === id) return "chef_hero";
      else if (this.value.sub_hero_one === id) return "sub_hero_one";
      else if (this.value.sub_hero_two === id) return "sub_hero_two";
      else return "";
    },
    // 点击武将
    clickHeroe(heroe) {
      // 判断武将是否被其他位置选中
      let select = this.heroeIsSelect(heroe.id);
      if (select && select !== this.fill) return;
      let value = JSON.parse(JSON.stringify(this.value));
      value[this.fill] = heroe.id;
      if (heroe.born_tactic_ids[0]) {
        let arr = [
          ["chef_hero", "chef_tactic_one", "chef_tactic_two"],
          ["sub_hero_one", "sub_one_tactic_one", "sub_one_tactic_two"],
          ["sub_hero_two", "sub_two_tactic_one", "sub_two_tactic_two"],
        ];
        let id = heroe.born_tactic_ids[0];
        arr.forEach((item) => {
          if (item[0] === this.fill) {
            if (value[item[1]] === id) value[item[1]] = null;
            else if (value[item[2]] === id) value[item[2]] = null;
          }
        });
      }
      this.$emit("change", value);
      this.$emit("next");
    },
  },
};
</script>

<style lang="scss" scoped>
.select-heroe {
  flex: 1;
  height: 0;
  margin-top: 0.1rem;
  border-radius: 4px;
  border: 1px solid #ebebeb;
  padding: 0.1rem 0.1rem 0.01rem;
  display: flex;
  flex-direction: column;
  background: #fff;
  .search {
    height: 0.34rem;
    border-radius: 4px;
    border: 1px solid #ebebeb;
    display: flex;
    align-items: center;
    input {
      flex: 1;
      width: 0;
      border: none;
      font-size: 0.14rem;
      color: #233d4d;
      &::placeholder {
        font-size: 0.12rem;
        color: #cccccc;
      }
    }
    .icon {
      width: 0.34rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 0.24rem;
      }
    }
    .clear {
      width: 0.3rem;
      height: 0.34rem;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      img {
        width: 0.22rem;
      }
    }
  }
  .country {
    display: flex;
    align-items: center;
    margin-top: 0.07rem;
    .item {
      width: 0.48rem;
      height: 0.28rem;
      background: #f2f2f2;
      border-radius: 0.05rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-right: 0.08rem;
      font-size: 0.12rem;
      &.active {
        background: #fff;
        border: 1px solid #ff8a69;
        background-image: url(../../../../assets/right_close.svg);
        background-repeat: no-repeat;
        background-position: right top;
      }
    }
    .flex {
      flex: 1;
    }
    .filter {
      width: 0.56rem;
      height: 0.28rem;
      border-radius: 0.04rem;
      box-sizing: border-box;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #ebebeb;
      font-size: 0.12rem;
      color: #233d4d;
      svg {
        margin-left: 0.06rem;
        width: 0.06rem;
        height: 0.04rem;
        transform: rotate(180deg);
        transition: all 0.3s;
      }
      &.active {
        color: #ff8a69;
        border: 1px solid #ff8a69;
        svg {
          transform: rotate(0deg);
        }
      }
    }
  }
  .quality {
    margin-top: 0.12rem;
    .tit {
      font-size: 0.12rem;
      color: #233d4d;
      line-height: 0.17rem;
    }
    .list {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-top: 0.05rem;
      .item {
        width: 0.72rem;
        height: 0.28rem;
        background: #f2f2f2;
        border-radius: 0.05rem;
        box-sizing: border-box;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 0.12rem;
        &.active {
          background: #fff;
          border: 1px solid #ff8a69;
          background-image: url(../../../../assets/right_close.svg);
          background-repeat: no-repeat;
          background-position: right top;
        }
      }
    }
  }
  .heroes-list {
    margin-top: 0.07rem;
    height: 0;
    flex: 1;
    .heroe {
      width: 0.48rem;
      display: inline-block;
      margin: 0.1rem 0 0;
      margin-left: 0.16rem;
      position: relative;
      &.heroe:nth-child(5n + 1) {
        margin-left: 0;
      }
      .img {
        width: 0.48rem;
        height: 0.48rem;
        img {
          width: calc(100% - 4px);
          height: calc(100% - 4px);
          border-radius: 0.04rem;
          &[type="4"] {
            border: 2px solid #f2ae32;
          }
          &[type="3"] {
            border: 2px solid #9363de;
          }
          &[type="2"] {
            border: 2px solid #7baefd;
          }
          &[type="1"] {
            border: 2px solid #99cfaa;
          }
        }
      }
      .radio {
        position: absolute;
        width: 0.14rem;
        height: 0.14rem;
        border-radius: 50%;
        display: none;
        justify-content: center;
        align-items: center;
        font-size: 0.08rem;
        top: 0.05rem;
        right: 0.05rem;
        &.active {
          display: flex;
          color: #fff;
          background: #ff8a69;
          border: 1px solid #ffffff;
          &.dis {
            color: #fff;
            background: #cdcdcd;
            border: 1px solid #ffffff;
          }
        }
      }
      .name {
        height: 0.2rem;
        position: relative;
        margin-top: 0.07rem;
        p {
          position: absolute;
          left: 50%;
          transform: translateX(-50%);
          font-size: 0.14rem;
          color: #333333;
          margin: 0;
          line-height: 0.2rem;
          white-space: nowrap;
          ::v-deep {
            span {
              color: #ff8a69;
            }
          }
        }
      }
    }
  }
}
</style>
